<?php
namespace app\api\controller;
use app\common\controller\Base;

class NeedPub extends Base{

     /**
     * [needInfo 需求详情]
     * @return [type] [description]
     */
    public function needInfo(){
        $rule = [
            'id' => 'require|number',
            'lng' => 'require',
            'lat' => 'require'
        ];

        $msg = [
            'id.require' => '参数id丢失',
            'lng.require' => '参数lng丢失',
            'lat.require' => '参数lat丢失'
        ];

        $validate = new \think\validate($rule,$msg);
        if($validate->check($this->post) === false){
            $this->apiReturn(0,$validate->getError());
        }

        
        
        $where['n.id'] = intval($this->post['id']);

    
        $info = model('common/need')->getInfo($where,trim($this->post['lng']),trim($this->post['lat']));


        if(is_null($info) || $info['status'] == 0){
            $this->apiReturn(0,'该需求不存在或已被删除');
        }

    
        //是否收藏
        $info['is_collect'] =  $info['is_order'] = $info['is_care'] = 0;
        if(isset($this->post['token']) && trim($this->post['token']) != ''){
            $this->user_check(trim($this->post['token']));
            if(db('collect')->where('type',1)->where('foreign_key',$info['id'])->where('user_id',UID)->find()){
                $info['is_collect'] = 1;
            }
            //是否接单
            if(db('need_apply')->where('apply_user',UID)->where('foreign_key',$info['id'])->find()){
                $info['is_order'] = 1;
            }
            //是否关注
            if(db('user_care')->where('user_id',UID)->where('care_id',$info['user_id'])->find()){
                $info['is_care'] = 1 ;
            }
        }
       
        
        $this->apiReturn(1,'详情',$info);
    }


     /**
     * [applyUser 需求申请者列表]
     * @return [type] [description]
     */
    public function applyUser(){
        if(!isset($this->post['id']) || !intval($this->post['id'])){
            $this->apiReturn(0,'参数id丢失');
        }
        $model = model('common/need');
        //该需求是否存在
        $need = $model->get(intval($this->post['id']));
        if(is_null($need) || $need['status'] == 0){
            $this->apiReturn(0,'该需求不存在或已被删除');
        }
        $list = $model->applyer($this->post['id'],$this->p);
        $this->apiReturn(1,'申请者列表',$list);
    }


}
